using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Restore
{
    public partial class frmRestore : Form
    {
        Boolean isExistDatabase = false;
        public frmRestore()
        {
            InitializeComponent();
        }
        // load form
        private void frmRestore_Load(object sender, EventArgs e)
        {
            txtDatabaseName.Text = "dbVehicle";
            controlForm(true, true, false, "Restore database", Color.Gray);
            txtSeverName.Focus();
        }
        // connect to sever
        private void btCheckConnecSever_Click(object sender, EventArgs e)
        {
            if (txtSeverName.Text.Trim() != string.Empty)
            {
                CheckConnection ckCnn = new CheckConnection();
                ckCnn.ServerName = txtSeverName.Text.Trim();
                ckCnn.CheckCnn();
                if (ckCnn.ResultServer)
                {
                    controlForm(false, true, true, "Restore database", Color.Gray);
                    txtSeverName.ReadOnly = true;

                    txtDatabaseName.Select();
                }
                else
                {
                    controlForm(true, true, false, "Connect to Sever fail", Color.Brown);
                    txtSeverName.Focus();
                }
            }
        }
        private void txtSeverName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
                btCheckConnecSever_Click(null, null);
        }
        // connect to database
        private void btConnectDatabase_Click(object sender, EventArgs e)
        {
            CheckConnection ckCnn = new CheckConnection();
            ckCnn.ServerName = txtSeverName.Text.Trim();
            ckCnn.Database = txtDatabaseName.Text.Trim();
            ckCnn.checkDatabase();
            if (ckCnn.ResultDB)
            {
                controlForm(false, false, true, "Database is exists", Color.Green);
                //txtDatabaseName.ReadOnly = true;
                isExistDatabase = true;
            }
            else
            {
                controlForm(false, true, false, "Database is not exists", Color.Red);
                // create new database
                //string cnnString = String.Format(@"Data Source={0};Integrated Security=true;Connect Timeout=2", ckCnn.ServerName);
                //string sqlCreateDatabase = "create database " + txtDatabaseName.Text.Trim();
                //int temp = ckCnn.executeNonQuery(cnnString, CommandType.Text, sqlCreateDatabase, null);
                //lbHelper.ForeColor = Color.Green;
                //lbHelper.Text = "create database successful";
            }
        }
        private void txtDatabaseName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
                btConnectDatabase_Click(null, null);
        }
        // restore database
        private void txtLinkFile_Click(object sender, EventArgs e)
        {
            openFileRestore.Title = "Browser file .Bak to restore database";
            openFileRestore.FileName = string.Empty;
            openFileRestore.Filter = "FileRestoreDatabase|*.Bak";
            if (openFileRestore.ShowDialog() == DialogResult.OK)
            {
                if (openFileRestore.FileName.Length > 0)
                {
                    txtLinkFile.Text = openFileRestore.FileName.Trim();
                }
            }
        }
        private void btRestoreData_Click(object sender, EventArgs e)
        {
            CheckConnection ckCnn = new CheckConnection();
            ckCnn.ServerName = txtSeverName.Text.Trim();
            ckCnn.Database = txtDatabaseName.Text.Trim();
            if (txtLinkFile.Text.Trim() != string.Empty)
            {
                string strConnect = string.Format("Data Source={0};Integrated Security=SSPI",txtSeverName.Text.Trim());
                SqlConnection conn = new SqlConnection(strConnect);
                conn.Open();
                string sqlRestoreDatabase = "RESTORE DATABASE " + txtDatabaseName.Text.Trim() + " " +
                                            "FROM DISK = '" + txtLinkFile.Text.Trim() + "' " +
                                            "WITH " +
                                               "MOVE '" + txtDatabaseName.Text.Trim() + "' TO 'D:\\" + txtDatabaseName.Text.Trim() + ".mdf' , " +
                                               "MOVE '" + txtDatabaseName.Text.Trim() + "_Log'  TO 'D:\\" + txtDatabaseName.Text.Trim() + ".ldf', REPLACE";
                SqlCommand command = new SqlCommand(sqlRestoreDatabase, conn);
                try
                {
                    int i = command.ExecuteNonQuery();
                    controlForm(false, false, false, "Restore database " + txtDatabaseName.Text.Trim() + " success", Color.Blue);
                }
                catch
                {
                    controlForm(false, false, false, "Restore database " + txtDatabaseName.Text.Trim() + " FAIL, please try again!", Color.Red);
                }
            }
        }
        // my function - control form
        public void controlForm(Boolean typeGroupSeverName, Boolean typeGroupDatabaseName, Boolean typeGroupChooseFile, string txtLbAlert, Color clLbAlert)
        {
            groupSevername.Enabled = typeGroupSeverName;
            groupDatabaseName.Enabled = typeGroupDatabaseName;
            groupChooseFile.Enabled = typeGroupChooseFile;

            lbAlert.ForeColor = clLbAlert;
            lbAlert.Text = txtLbAlert;
        }

    }
}